﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LoginForm
{
    public partial class NewPupil : Form
    {
        public NewPupil()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string realname = txtRealName.Text;
            string nickname = txtNickName.Text;
            string education = txtEducation.Text;
            string school = txtrSchool.Text;
            string schedule = txtrSchedule.Text;
            string strMoney = txtmMoney.Text;
            string strId = txtmId.Text;
            string strPhone = txtmPhone.Text;
            if (Validation.IsEmpty(strPhone)) {
                strPhone = "0000000000";
            }
            string[] arr = new string[] { realname, nickname, education, school, schedule, strMoney,strId,strPhone };
            if (Validation.IsValidTextBox(arr))
            {
                int? id = Convert.ToInt32(strId);
                string[] arrMoney = strMoney.Split(',');
                string str = arrMoney[0] + arrMoney[1];
                float? money = Convert.ToInt64(str);
                Pupil p = new Pupil()
                {
                    Id = id,
                    Realname = realname,
                    Nickname = nickname,
                    Education = education,
                    School = school,
                    Schedule = schedule,
                    Money = money,
                    Phone = strPhone
                };
                bool b = SunnyIO.AddNewPupil(FileName.pupil, p);
                if (b) {
                    MessageBox.Show("Thêm Thành Công", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.Dispose();
                } else {
                    MessageBox.Show("Thêm Thất Bại: Trùng ID", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else {
                MessageBox.Show("Hãy Nhập Đầy Đủ Thông Tin", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
